import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list as deviceTypeList } from '../deviceType/deviceType.api';
import { list as serialList } from '../serial/serial.api';
import { getDeviceModelByType } from '../deviceModel/deviceModel.api';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { buildUUID } from '/@/utils/uuid';

export const rtuId = 24; //固化设备类型 MODBUS RTU类型编号，此处需要优化

export const columns: BasicColumn[] = [
  {
    title: 'DTU名称',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: 'DTU编号',
    dataIndex: 'deviceSn',
    width: 100,
  },
  {
    title: '设备类型',
    dataIndex: 'deviceTypeName',
    width: 120,
  },
  {
    title: '设备型号',
    dataIndex: 'modelName',
    width: 80,
  },
  {
    title: 'IP地址',
    dataIndex: 'ip',
    width: 100,
  },
  {
    title: 'DTU端口',
    dataIndex: 'port',
    width: 100,
  },
  {
    title: '上/下线时间',
    dataIndex: 'switchTime',
    width: 100,
  },
  {
    title: 'DTU状态',
    dataIndex: 'status',
    width: 80,
    slots: { customRender: 'status' },
    // customRender: ({ text }) => {
    //   return render.renderDict(text, 'iot_device_status');
    // },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '设备名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '设备编号',
    field: 'deviceSn',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '设备类型',
    field: 'deviceTypeId',
    component: 'ApiTreeSelect',
    componentProps: {
      api: deviceTypeList,
      params: {alias: 'MODBUS'},
      immediate: true,
      expandAll: true,
      resultField: 'data',
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    label: '设备状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'iot_device_status',
      placeholder: '请选择DTU状态',
      stringToNumber: false,
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '设备名称',
    field: 'name',
    component: 'Input',
    required: true,
  },
  {
    label: '设备编号',
    field: 'deviceSn',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.deviceTypeId != rtuId,
  },
  {
    label: '串口',
    field: 'deviceSn',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: serialList,
      labelField: 'label',
      valueField: 'value',
      showSearch: true,
      dropdownStyle: {
        maxHeight: '50vh',
      },
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => values.deviceTypeId == rtuId,
  },
  {
    label: 'IP地址',
    field: 'ip',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.deviceTypeId != rtuId,
  },
  {
    label: '端口',
    field: 'port',
    component: 'InputNumber',
    componentProps: {
      //步数
      step: 1,
    },
    ifShow: ({ values }) => values.deviceTypeId != rtuId,
  },
  {
    label: '设备类型',
    field: 'deviceTypeId',
    component: 'ApiTreeSelect',
    required: true,
    componentProps: {
      api: deviceTypeList,
      params: {alias: 'MODBUS'},
      immediate: true,
      expandAll: true,
      resultField: 'data',
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    label: '设备型号',
    field: 'model',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: getDeviceModelByType,
        params: {typeId: formModel.deviceTypeId},
        labelField: 'model',
        valueField: 'id',
        getPopupContainer: () => document.body,
      };
    },
  },  
];

export const debugColumns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: '设备地址',
    dataIndex: 'childSn',
    width: 60,
  },
  {
    title: '数据格式',
    dataIndex: 'dataFormat',
    width: 60,
  },
  {
    title: '指令',
    dataIndex: 'cmd',
    width: 60,
    slots: { customRender: 'cmd' },
  },
  {
    title: '值类型',
    dataIndex: 'type',
    width: 80,
    slots: { customRender: 'type' },
  },
  {
    title: '起始地址',
    dataIndex: 'start',
    width: 60,
    slots: { customRender: 'start' },
  },
  {
    title: '自定义报文',
    dataIndex: 'message',
    width: 160,
    slots: { customRender: 'message' },
  },
];


export const searchDebugFormSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '设备地址',
    field: 'childSn',
    component: 'Input',
    colProps: { span: 6 },
  },
];

export const ProtocolType = [
  {label: '自定义', value: "cus"},
  {label: 'modbus:rtu', value: "rtu"},
  {label: 'modbus:tcp', value: "tcp"},
]

export const CmdOptions = [
  {label: '读01', value: "01"},
  {label: '读02', value: "02"},
  {label: '读03', value: "03"},
  {label: '读04', value: "04"},
  {label: '写05', value: "05"},
  {label: '写06', value: "06"},
  {label: '写10', value: "10"},
  {label: '写0F', value: "0F"},
];

export const TypeOptions = [
  {label: 'int', value: 'int'},
  {label: 'short', value: 'short'},
  {label: 'long', value: 'long'},
  {label: 'float', value: 'float'},
  {label: 'double', value: 'double'},
  {label: 'string', value: 'string'},
  {label: 'boolean', value: 'boolean'},
]

export const CmdCusOptions = [
  {label: '读', value: "read"},
  {label: '写(设备会响应)', value: "write"},
  {label: '异步写(设备不会响应)', value: "writeOfAsync"},
]